﻿using System.Collections.Generic;
using System.Linq;
using Petshop.Backend.Data;

namespace Petshop.Backend.Logic
{
    public class CatalogService
    {

        private readonly IDatabaseSession _databaseSession;

        public CatalogService()
            : this(new DatabaseSession())
        {
        }
        public CatalogService(IDatabaseSession databaseSession)
        {
            _databaseSession = databaseSession;
        }
        public Category GetCategoryById(string categoryId)
        {
            if (string.IsNullOrEmpty(categoryId))
                return null;
            using (var rep = new CategoryRepository(_databaseSession))
            {
                return rep.FindById(categoryId);
            }
        }


        public List<Product> GetProductsByCategory(string categoryId)
        {
            if (string.IsNullOrEmpty(categoryId))
                return null;
            using (var rep = new ProductRepository(_databaseSession))
            {
                return rep.RetrieveProductByCategeoryId(categoryId).ToList();
            }
           
        }


        public List<Product> GetProductsBySearch(string text)
        {
            if (string.IsNullOrEmpty(text))
                return null;
            using (var rep = new ProductRepository(_databaseSession))
            {
                return rep.FindProductBy(text).ToList();
            }
        }


        public List<Item> GetItemsByProduct(string productId)
        {
            if (string.IsNullOrEmpty(productId))
                return null;
            using (var rep = new ItemRepository(_databaseSession))
            {
                return rep.RetrieveByProductId(productId).ToList();
            }
        }


        public Item GetItemInfoById(string itemId)
        {
            if (string.IsNullOrEmpty(itemId))
                return null;
            using (var rep = new ItemRepository(_databaseSession))
            {
                return  rep.RetrieveById(itemId);
              
            }

        }
    }
}